package com.seirsoft.framework.util;

public class UnicodeString {
	private final static char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'A', 'B', 'C', 'D', 'E', 'F' };
	
	public static String byteToHex(byte b) {
		// Returns hex String representation of byte b
		char[] array = { 
				hexDigit[(b & 0xF0) >>> 4], 
				hexDigit[b & 0x0F] 
		};
		return new String(array);
	}

	public static String charToHex(char c) {
		// Returns hex String representation of char c
		
		// "无符号"右移位运算符（>>>），它使用了"零扩展"：无论正负，都在高位插入0
		char[] array = { 
				hexDigit[(c & 0xF000) >>> 12], 
				hexDigit[(c & 0x0F00) >>> 8],
				hexDigit[(c & 0x00F0) >>> 4], 
				hexDigit[c & 0x000F]
		};
		
		return new String(array);
	}
	
	public static String getUnicodeString(String strInput)
	{
		StringBuffer buffer = new StringBuffer();
		for(int i=0; i<strInput.length(); i++)
		{
			char b = strInput.charAt(i);
			String strA = UnicodeString.charToHex(b);
			buffer.append('\\');
			buffer.append("u");
			buffer.append(strA);
		}
		
		return buffer.toString();
	}
}
